﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Basics.Models.Extend.Dtos.Schedule;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 日程安排
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.11.29 
    /// </summary>
    [Route("api/Extend/[controller]")]
    public class ScheduleController : BaseApiController
    {
        private ScheduleBll scheduleBll = new ScheduleBll();

        #region GET
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseResult> GetList([FromQuery] ScheduleListQuery input)
        {
            var data = (await scheduleBll.GetList(input.startTime, input.endTime)).MapToList<ScheduleListOutput>();
            return Success(new { list = data });
        }
        /// <summary>
        /// app
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("AppList")]
        [AllowAnonymous]
        public async Task<ResponseResult> GetAppList([FromQuery] ScheduleListQuery input)
        {
            var days = new Dictionary<string, int>();
            var scheduleList = (await scheduleBll.GetList(input.startTime, input.endTime)).MapToList<ScheduleListOutput>();
            foreach (var item in GetAllDays(input.startTime.ToDate(), input.endTime.ToDate()))
            {
                var _startTime = item.ToString("yyyy-MM-dd") + " 23:59";
                var _endTime = item.AddDays(1).ToString("yyyy-MM-dd") + " 00:00";
                var count = scheduleList.FindAll(m => m.startTime <= _startTime.ToDate() && m.endTime >= _endTime.ToDate()).Count;
                days.Add(item.ToString("yyyyMMdd"), count);
            }
            var today_startTime = DateTime.Now.ToString("yyyy-MM-dd") + " 23:59";
            var today_endTime = DateTime.Now.ToString("yyyy-MM-dd") + " 00:00";
            var data = new
            {
                signList = days,
                todayList = scheduleList.FindAll(m => m.startTime <= today_startTime.ToDate() && m.endTime >= today_endTime.ToDate())
            };
            return Success(data);
        }

        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ResponseResult> GetInfo(string id)
        {
            var data = (await scheduleBll.GetInfo(id)).MapTo<ScheduleInfoOutput>();
            return Success(data);
        }
        #endregion

        #region POST
        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Create([FromBody] ScheduleCrInput input)
        {
            var entity = input.MapTo<ScheduleEntity>();
            await scheduleBll.Create(entity);
            return Success("新建成功");
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(string id, [FromBody] ScheduleUpInput input)
        {
            var entity = input.MapTo<ScheduleEntity>();
            await scheduleBll.Update(id, entity);
            return Success("更新成功");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(string id)
        {
            var entity = await scheduleBll.GetInfo(id);
            if (entity != null)
            {
                await scheduleBll.Delete(entity);
            }
            return Success("删除成功");
        }
        #endregion

        #region Method
        /// <summary>
        /// 获取固定日期范围内的所有日期，以数组形式返回
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        private DateTime[] GetAllDays(DateTime startTime, DateTime endTime)
        {
            var listDay = new List<DateTime>();
            DateTime dtDay = new DateTime();
            //循环比较，取出日期；
            for (dtDay = startTime; dtDay.CompareTo(endTime) <= 0; dtDay = dtDay.AddDays(1))
            {
                listDay.Add(dtDay);
            }
            return listDay.ToArray();
        }
        #endregion
    }
}
